﻿namespace WG3000_COMM.Common
{
    using System;

    public class wgControllerInfo
    {
        private const int COMM_UDP_PORT = 0xea60;
        private int m_ControllerSN;
        private string m_IP = "";
        private int m_PORT = 0xea60;
        public string ReceivedBytes = "";
        public DateTime UpdateDateTime = DateTime.Now;

        public static int GetControllerType(int controllerSN)
        {
            if (controllerSN > 0x5f5e100)
            {
                if (controllerSN <= 0xbebc1ff)
                {
                    return 1;
                }
                if (controllerSN <= 0x11e1a2ff)
                {
                    return 2;
                }
                if (controllerSN <= 0x17d783ff)
                {
                    return 0;
                }
                if (controllerSN <= 0x1dcd64ff)
                {
                    return 4;
                }
            }
            return 0;
        }

        public static bool IsElevator(int controllerSN) => 
            ((controllerSN >= 0xa21fe80) && (controllerSN <= 0xaba94ff));

        public void update(uint sn, string ip, int port, DateTime dt)
        {
            this.ControllerSN = (int) sn;
            this.IP = ip;
            this.PORT = port;
            this.UpdateDateTime = dt;
        }

        public void update(uint sn, string ip, int port, DateTime dt, string recv)
        {
            this.ControllerSN = (int) sn;
            this.IP = ip;
            this.PORT = port;
            this.UpdateDateTime = dt;
            this.ReceivedBytes = recv;
        }

        public int ControllerSN
        {
            get => 
                this.m_ControllerSN;
            set
            {
                if ((value == -1) || (value == 0x3b9ac9ff))
                {
                    this.m_ControllerSN = -1;
                }
                else
                {
                    this.m_ControllerSN = value;
                }
            }
        }

        public string IP
        {
            get => 
                this.m_IP;
            set
            {
                this.m_IP = value;
            }
        }

        public int PORT
        {
            get => 
                this.m_PORT;
            set
            {
                if ((value >= 0) && (value < 0xffff))
                {
                    this.m_PORT = value;
                }
            }
        }
    }
}

